package com.haobo.test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * ClassName:GetsqlSessionFactory
 * Package:com.haobo.test
 * Description:
 *
 * @Date:2021/6/2 11:36
 * @Author:haobo
 */
public class GetsqlSessionFactory {
    public static SqlSession getSqlSession() throws IOException {
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得session回话对象     如果为true  自动提交事务不需要手动commit
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        return sqlSession;
    }
}
